package com.task.control.task;

import com.task.controller.TaskController;
import com.task.controller.control.Control;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Arrays;

public class TaskDispatch {

    private static HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.INFO, 0, TaskDispatch.class.getCanonicalName());

    private static TaskDispatch taskDispatch = new TaskDispatch();

    private Context context;

    private TaskController taskController;

    private Control control;

    private TaskDispatch() {
        super();
    }

    public static TaskDispatch getInstance() {
        if (taskDispatch == null) {
            taskDispatch = new TaskDispatch();
        }
        return taskDispatch;
    }

    public void init(Context context) {
        this.context = context;
        if (taskController == null) {
            taskController = TaskController.getInstance().init(context);
        }
        if (control == null) {
            control = taskController.getControl();
        }
    }

    public void execute(String resource, Object... object) {
        control.execute(resource, object, objects -> {
            if (objects == null) {
                HiLog.info(LABEL_LOG, "执行的资源没有返回值");
            } else {
                HiLog.info(LABEL_LOG, Arrays.toString(objects));
            }
        });
    }
}
